//
//  BenefitMsgViewController.m
//  LiveStar
//
//  Created by 漪珊 on 2017/4/19.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "BenefitMsgViewController.h"
#import "BenefitPaperCell.h"
#import "BenefitPresentCell.h"
#import "BenefitModel.h"

@interface BenefitMsgViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *_benefitArray;
}

@property (nonatomic, strong) UITableView *tableView;
@end

@implementation BenefitMsgViewController

- (void)BenefitRequest {
    NSDictionary *dict = @{@"udid":User_ID,
                           @"token":TOKEN,
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kBenefitMsg];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            NSArray *array = responseObject[@"data"][@"list"];
            for (NSDictionary *benefitDic in array) {
                BenefitModel *model = [[BenefitModel alloc] initWithDict:benefitDic];
                [_benefitArray addObject:model];
            }
            if (_benefitArray.count>0) {
                [self creatTableView];
                [self.tableView reloadData];                
            }
        }
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpUI];
    [self BenefitRequest];
    self.navigationItem.title = @"福利";
}

#pragma mark -- UI
- (void)setUpUI {
    _benefitArray = [NSMutableArray arrayWithCapacity:1];
}

- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.tableView];
}

#pragma mark -- tableView 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BenefitModel *model = _benefitArray[indexPath.row];
    if ([model.o_type intValue] == 10) { //红包消息
        BenefitPaperCell *cell = [BenefitPaperCell loadNibCellWithTabelView:tableView];
        cell.benefitModel = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    BenefitPresentCell *cell = [BenefitPresentCell loadNibCellWithTabelView:tableView];
    cell.callAction = ^(){
        
    };
    cell.benefitModel = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

#pragma mark -- 区头视图

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    BenefitModel *model = _benefitArray[indexPath.row];
    if ([model.o_type intValue] == 10) {
        return 145;
    }
    return 260;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
